<template>
  <a-card :bordered='false'>
    <search :config='searchConfig' @getHeight='getHeight' @search='search' />
    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        v-bind='vexConfig'
        :align='left'
        :border='true'
        :checkbox-config='checkboxConfig'
        :columns='columns'
        :data='dataSource'
        :height='pageHeight-seachHeight-94'
        :loading='loading'
        :resizable='true'
        :showOverflow='true'
        :toolbar-config='tableToolbar'
        size='mini'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>

            <a-button @click='handleAdd' type='primary'>新增</a-button>
            <!--<a-button type="primary" icon="start" @click="startorstop(1)">启用</a-button>-->
            <!--<a-button type="primary"  @click="startorstop(0)">停用</a-button>-->
            <a-button type='primary' @click='batchDel'>删除</a-button>
            <a-button type='primary' @click="handleExportXls('职位管理')">模板下载</a-button>
            <a-upload :action='importExcelUrl' :headers='tokenHeader1' :multiple='false' :showUploadList='false'
                      name='file' @change='handleImportExcel'>
              <a-button type='primary'>导入</a-button>
            </a-upload>
            <a-button type='primary' @click="downExportXls('cn_position_report')">导出</a-button>

          </div>
        </template>
        <template #detail='{ row,rowIndex }'>
          <a href='javascript:' @click='handleDetail(row)'>{{ row.positionCode }}</a>
        </template>
        <template #status='{ row,rowIndex }'>
          <a-switch :checked='row.status' size='small' @change='handleStatus(row,$event)' />
        </template>

        <template #isLeader='{ row }'>
          {{ row.isLeader == 1 ? '是' : '否' }}
        </template>
        <template #action='{ row,record }'>
          <a @click='handleEdit(row)'>编辑</a>
                    <a-divider type="vertical"/>
          <!-- <a v-has="'business:log'" href="javascript:;" @click='handleBusiness("职位管理",row)'>日志</a>-->

            <a-dropdown>
              <a class='ant-dropdown-link'>
                更多
                <a-icon type='down' />
              </a>
              <a-menu slot='overlay'>
              <a-menu-item>
                <a href='javascript:' @click='handleConfig(row)'>查看下属</a>
              </a-menu-item>
              <a-menu-item v-has="'business:log'">
                <a href='javascript:' @click='handleBusiness("职位管理",row)'>日志</a>
              </a-menu-item>
              </a-menu>
            </a-dropdown>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <sysPosition-modal ref='modalForm' @ok='modalFormOk'></sysPosition-modal>
    <business-log ref='businessLog'></business-log>
    <position-list-ter ref="ConfigTer" :positionCode='positionCode' ></position-list-ter>
  </a-card>
</template>

<script>
import SysPositionModal from '@page-modules/SysPositionModal'
import { PageListMixin } from '@/mixins/PageListMixin'
import { ajaxGetDictItems } from '@api/api'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { getAction } from '@/api/manage'
import PositionListTer from './modules/PositionListTer'

export default {
  name: 'PositionList',
  mixins: [PageListMixin],
  components: {
    SysPositionModal,
    PositionListTer
  },
  data() {
    const self = this
    return {
      positionCode:'', //查询下属所需组织
      tokenHeader1: { 'Authorization': 'Bearer ' + Vue.ls.get(ACCESS_TOKEN).access_token },
      description: '职位表管理页面',
      searchConfig: [
        {
          type: 'tree-select',
          dataType: 'org',
          name: 'orgCode',
          placeholder: '所属组织'
        },
        {
          type: 'input',
          minWidth: 80,
          name: 'positionCode',
          placeholder: '职位编码'
        },
        {
          type: 'input',
          name: 'positionName',
          placeholder: '职位名称'
        },
        {
          type: 'j-select',
          dictCode: 'position_level',
          name: 'positionLevel',
          placeholder: '职级'
        },
        {
          type: 'input',
          name: 'userName',
          placeholder: '用户账号'
        }, {
          type: 'input',
          name: 'realName',
          placeholder: '用户姓名'
        }
      ],
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: 'left' },
        {
          title: '职位编码',
          minWidth: 80,
          field: 'positionCode',
          slots: { default: 'detail' }
        },
        {
          title: '职位名称',
          minWidth: 80,
          field: 'positionName'
        },
        // {
        //   title: '是否部门负责人',
        //   minWidth: 110,
        //   field: 'isLeader',
        //   slots: { default: 'isLeader' }
        // },
        {
          title: '职级',
          minWidth: 80,
          field: 'positionLevel_dictText'
        },
        {
          title: '上级职位编码',
          minWidth: 100,
          field: 'parentCode'
        },
        {
          title: '上级职位名称',
          minWidth: 100,
          field: 'parentName'
        },
        {
          title: '组织',
          minWidth: 80,
          field: 'departName'
        },
        {
          title: '用户账号',
          minWidth: 80,
          field: 'userName'
        },
        {
          title: '用户姓名',
          minWidth: 80,
          field: 'realName'
        },
        {
          title: '创建人',
          minWidth: 80,
          field: 'createName'
        },
        {
          title: '创建时间',
          minWidth: 80,
          field: 'createTime'
        },
        {
          title: '更新人',
          minWidth: 80,
          field: 'updateName'
        },
        {
          title: '更新时间',
          minWidth: 80,
          field: 'updateTime'
        },
        {
          title: '状态',
          minWidth: 80,
          fixed: 'right',
          field: 'status',
          slots: { default: 'status' }
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          slots: { default: 'action' },
          minWidth: 120
        }
      ],
      positionLevels: [],
      isorter: {
        permflag: 'position_page_impl'
      },
      url: {
        list: window._CONFIG['domianURL'] + '/cnPositionController/getPageList',
        delete: window._CONFIG['domianURL'] + '/cnPositionController/deleteByid',
        deleteBatch: window._CONFIG['domianURL'] + '/cnPositionController/deleteBatch',
        exportXlsUrl: window._CONFIG['domianURL'] + '/cnPositionController/exportXls',
        importExcelUrl: window._CONFIG['domianURL'] + '/cnPositionController/importExcel',
        startOrStopUrl: window._CONFIG['domianURL'] + '/cnPositionController/startOrStop'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${this.url.importExcelUrl}`
    }
  },
  methods: {
    //查看下属
    handleConfig(userName) {
      this.$refs.ConfigTer.showModal(userName)
      this.$refs.ConfigTer.positionCode = userName.positionCode
      this.$refs.ConfigTer.loadData(1)
      console.log(this.positionCode)
      },
    startorstop: function(status) {
      if (!this.url.startOrStopUrl) {
        this.$message.error('请设置url')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择数据')

      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认修改',
          content: '是否修改选中数据?',
          onOk: function() {
            that.loading = true
            getAction(that.url.startOrStopUrl, { ids: ids, status: status }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loadData()
            })
          }
        })
      }
    },
    initDictConfig() {
      return ajaxGetDictItems('position_level').then(res => {
        if (res.success) this.positionLevels = res.result
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
